#include "exceptions.h"

namespace png8crop
{
	ex_generic::ex_generic(const _TCHAR *msg)
		:fmt( _T("%s\n")),
		msg(NULL),
		msg2(NULL)
	{
	}

	ex_generic::ex_generic(const _TCHAR *fmt, const _TCHAR *msg, const _TCHAR *msg2) 
		:fmt(fmt),
		msg(new _TCHAR[max_msg_len]),
		msg2(new _TCHAR[max_msg_len])
	{
		msgcpy(this->msg, max_msg_len, msg);
		msgcpy(this->msg2, max_msg_len, msg2);
	}

	ex_generic::~ex_generic()
	{
		if(this->msg != NULL)
		{
			delete[] this->msg;
			this->msg = NULL;
		}
		
		if(this->msg2 != NULL)
		{
			delete[] this->msg2;
			this->msg2 = NULL;
		}
	}

	const _TCHAR *const ex_generic::get_msg() const
	{
		return this->msg;
	}

	const _TCHAR *const ex_generic::get_msg2() const
	{
		return this->msg2;
	}

	void ex_generic::print_msg() const
	{
		_ftprintf(stderr, this->fmt, this->msg, this->msg2);
	}

	void ex_generic::msgcpy(_TCHAR * dst, int max_len, const _TCHAR* src)
	{
		if(dst == NULL)
		{
			return;
		}

		if(src != NULL)
		{
			_TCHAR *dst_end = dst + max_len - 1;
			while(*src && dst != dst_end) *dst++ = *src++;
		}

		*dst = _T('\0');
	}
}